<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

    <script>
        //json字符串和json对象的转换
        //eval转换单个json字符串


        //eval是语法不严格的函数- 不太在意json是不是一个合法的json
        //忽略key-value是使用单引号还是双引号(才是合法的json)
        let str1='{"id":"1","name":"tom"}';
        let str1_obj=eval("("+str1+")");
        console.log(typeof(str1_obj));
        console.log(str1_obj.name);

        let str2="{'id':'1','name':'admin'}";
        let str2_obk=eval("("+str2+")");
        console.log(typeof(str2_obk));
        console.log(str2_obk.name);

        //eval转成字符串形式的json数组
        let str3='[{"id":2,"name":"james"},{"id":2,"name":"james"}]';
        let str3_obj=eval(str3);
        console.log(typeof(str3_obj));
        console.log(str3_obj[1].name);

        let str4 = "[{'id':2,'name':'james'},{'id':2,'name':'james'}]";
        let str4_obj = eval(str4);
        console.log(typeof(str4_obj));
        console.log(str4_obj[1].name);

        let str5='{"id":"1","name":"tom2"}';//合法的
        let str5_obj=JSON.parse(str5);
        console.log(typeof(str5_obj));
        console.log(str5_obj.name);

        //JSON.parse转换对json格式的字符串要求必须是合法[使用双引号]
        // let str6 = "{'id':'1','name':'admin2'}";;//不合法的
        // let str6_obj = JSON.parse(str6);
        // console.log(typeof(str6_obj));
        // console.log(str6_obj.name);

        //json对象如何转换成json字符串
        let str77_obj=[{"id":20,"name":"tom"},{"id":20,"name":"tom"}];
        let str7_str=JSON.stringify(str77_obj);

        console.log(typeof(str7_str));
        console.log(str7_str);

    </script>

</body>
</html>